package com.example.juc;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;

public class CopyOnWriterListTest {
    public static void main(String[] args) throws InterruptedException {
//        CopyOnWriteArrayList<Integer> strings = new CopyOnWriteArrayList<>();
//        List<Object> strings = Collections.synchronizedList(new ArrayList<>());
//        CopyOnWriteArraySet<String> strings = new CopyOnWriteArraySet<>();
        Set<Object> strings = Collections.synchronizedSet(new HashSet<>());
        new Thread(()->{
            for (int i1 = 0; i1 < 10; i1++) {
                strings.add(i1);
            }
        }).start();
        new Thread(()->{
            for (int i1 = 10; i1 < 20; i1++) {
                strings.add(i1);
            }
        }).start();


        new Thread(()->{
            for (int i1 = 20; i1 < 30; i1++) {
                strings.add(i1);
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println(strings);
    }
}
